# -*- encoding: utf-8 -*-
#import odoo.addons.decimal_precision as dp
from odoo import models, fields, api, _
from odoo.addons import base_cw
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_round, float_compare


class account_trial_balance(models.Model):
    _name = 'account.trial.balance'
    _description = u'试算平衡'
    _rec_name = 'account_id'

    period_id = fields.Many2one('account.period', u'期别')
    account_id = fields.Many2one('cncw.account', u'会计科目', ondelete="restrict")
    begin_debit = fields.Float(u'期初借方', digits='Amount')
    begin_credit = fields.Float(u'期初贷方', digits='Amount')
    debit = fields.Float(u'本期借方发生额', digits='Amount')
    credit = fields.Float(u'本期贷方发生额', digits='Amount')
    end_debit = fields.Float(u'期末借方', digits='Amount')
    end_credit = fields.Float(u'期末贷方', digits='Amount')


    """
    余额试算平衡表计算方法如下：
        全部账户的 借方期初余额合计数 等于     全部账户的贷方期初余额合计数；
        全部账户的 借方发生额合计     等于     全部账户的贷方发生额合计；
        全部账户的 借方期末余额合计   等于     全部账户的贷方期末余额合计。
    """
    def init(self):
        self._cr.execute("""
-- Function: public.account_trial_balance_settle(integer, integer)

-- DROP FUNCTION public.account_trial_balance_settle(integer, integer);

CREATE OR REPLACE FUNCTION public.account_trial_balance_settle(
    period_id_t integer,
    uid integer)
  RETURNS void AS
$BODY$
                        ---- drop function account_trial_balance_settle(integer,integer)
                        -- select account_trial_balance_settle(14,1)
                        begin

                            CREATE temp TABLE account_trial_balance_t
                            (
                              period_id integer, -- 期别
                              account_id integer, -- 会计科目
                              begin_debit numeric, -- 期初借方(原币)
                              begin_credit numeric, -- 期初贷方(原币)
                              debit numeric, -- 本期借方发生额(原币)
                              credit numeric, -- 本期贷方发生额(原币)
                              end_debit numeric, -- 期末借方(原币)
                              end_credit numeric -- 期末贷方(原币)
                            );
                            insert into account_trial_balance_t(period_id,account_id,begin_debit,begin_credit,debit,credit,end_debit,end_credit)
                            select period_id,account_id,lc_begin_debit,lc_begin_credit,lc_debit as debit,lc_credit as credit,lc_balance_debit,lc_balance_credit
                              from account_balance  a left join cncw_account b on a.account_id=b.id
                             where period_id=period_id_t
                               and a.sub_account_id is null
                               and coalesce(b.active,'f')='t'
                               and a.account_id is not null
                               and b.sub_account_type in ('none','has');

                            delete from account_trial_balance_t
                             where coalesce(begin_debit,0)=0.0 and coalesce(begin_credit,0)=0.0 and coalesce(debit,0)=0.0 and coalesce(credit,0)=0.0 and coalesce(end_debit,0)=0.0 and coalesce(end_credit,0)=0.0;

                            delete from account_trial_balance ;

                            insert into account_trial_balance(id,period_id,account_id,begin_debit,begin_credit,debit,credit,end_debit,end_credit)
                             select nextval('account_trial_balance_id_seq'),period_id,account_id,begin_debit,begin_credit,debit,credit,end_debit,end_credit
                               from account_trial_balance_t;

                            drop table account_trial_balance_t;
                         end;
                         $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;


        """)
